Skip to main content

Primitive Types in Java

In Java, primitive types are the most basic data types that are not objects. They are predefined by the language and named by a reserved keyword. Java has eight primitive data types:

  1. Integer types: byte, short, int (default), long
  2. Floating-point types: float, double (default)
  3. Character type: char
  4. Boolean type: boolean

Details

  • int - 32-bit signed two's complement integer. Default type for integer values.
  • float - 32-bit floating point. Used to save memory in large arrays of floating point numbers. Not recommended for precise values.
  • long - 64-bit signed two's complement integer. Use 'L' suffix to denote long literals.
  • double - 64-bit floating point. Default type for decimal values. More precise than float.

Literals in Java

Literals are the fixed values assigned to variables in Java. They represent data directly in the source code. Java supports several types of literals corresponding to its primitive data types:

  1. Integer Literals: Used for byte, short, int, and long types. By default, integer literals are of type int. To specify a long literal, append an 'L' or 'l' to the number (e.g., 100L). Don't use 'l' as it can be confused with the digit '1'.
  2. Floating-Point Literals: Used for float and double types. By default, floating-point literals are of type double. To specify a float literal, append an 'F' or 'f' to the number (e.g., 10.5f).
  3. Character Literals: Used for the char type. Character literals are enclosed in single quotes (e.g., 'A', '1', '\n').
  4. Boolean Literals: Used for the boolean type. The only two boolean literals are true and false.
  5. String Literals: Although not a primitive type, string literals are sequences of characters enclosed in double quotes (e.g., "Hello, World!").

Examples of Primitive Type Declarations

PrimitiveTypes.java
byte byteValue  = 100;  // 8-bit signed integer
short shortValue = 10000; // 16-bit signed integer

int age = 25; // 32-bit signed integer
int count = 1_000_000; // 32-bit signed integer with underscores for readability
int hexValue = 0x1A; // Hexadecimal literal
int binValue = 0b1010; // Binary literal

long longValue = 100000L; // 64-bit signed integer
float floatValue = 10.5f; // 32-bit floating point

double price = 20.99; // 64-bit floating point
double scientific = 1.23e4; // Scientific notation
double coeff = 23_456.75_01; // 64-bit floating point with underscores
double likeInt = 100D; // 64-bit floating point with 'D' suffix

char a = 'A'; // 16-bit Unicode character
boolean isActive = true; // boolean value

String greeting = "Hello, World!"; // String literal - not a primitive type

Here is a summary table of the primitive types in Java

TypeSizeRangeDescription
byte8 bits-128 to 127Used to save memory in large arrays.
short16 bits-32,768 to 32,767Also used to save memory in large arrays, but has a larger range than byte.
int32 bits-2,147,483,648 to 2,147,483,647The default data type for integer values.
long64 bits-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807Used when a wider range than int is needed.
float32 bitsApproximately ±3.40282347E+38F (6-7 significant decimal digits)Used to save memory in large arrays of floating point numbers. Not recommended for precise values.
double64 bitsApproximately ±1.79769313486231570E+308 (15 significant decimal digits)The default data type for decimal values. More precise than float. But use BigDecimal for precise values like healthcare and banking.
char16 bits'\u0000' (0) to '\uffff' (65,535)Used to store a single 16-bit Unicode character.
booleantrue or falseUsed for simple flags that track true/false conditions.

Key Nuances to Remember

  • The char is Unsigned: Unlike the integer types (byte, short, int, long) which are signed (allowing negative numbers), the char type is unsigned. It ranges from to 0 to 65,535.
  • Boolean Size: The JVM usually uses 1 byte to represent a boolean and a 4-byte (word) for boolean arrays. It is conceptually 1 bit, but memory is addressed in bytes.
  • Integer Overflow: If you add 1 to the maximum value of an int (2,147,483,647), it wraps around to the minimum value (-2,147,483,648). This is due to the two's complement representation.
  • Primitive vs. Wrapper Classes: For every primitive type, Java provides a corresponding Wrapper Class (e.g., int has Integer, char has Character). You’ll need these when working with Collections like ArrayList or HashMap, which cannot store primitives directly.